//
//  FilterManager.m
//  Task Views
//
//  Created by Chris Karr on 3/15/09.
//  Copyright 2009 Chris J. Karr. All rights reserved.
//

#import "FilterManager.h"
#import "FilterRowTemplate.h"

@implementation FilterManager


- (IBAction) filter:(id) sender
{
	[filterWindow makeKeyAndOrderFront:sender];
}

- (IBAction) resetPredicate:(id) sender
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	NSPredicate * predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:
																				  [NSPredicate predicateWithFormat:@"(%K != %@)", @"name", @"Task", nil]]];
		
	[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:predicate] forKey:@"task_predicate"];
}

- (NSArray *) fieldNames
{
	NSSortDescriptor * sort = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
	
	NSArray * fieldsList = [[fields arrangedObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
	
	NSMutableArray * names = [NSMutableArray array];
	for (id name in fieldsList)
		[names addObject:[name valueForKey:@"name"]];
	
	return names;
}

- (void) observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
	NSMutableArray * ruleTemplates = [NSMutableArray arrayWithArray:[editor rowTemplates]];
	
	NSPredicateEditorRowTemplate * original = [ruleTemplates lastObject];
	
	NSMutableSet * lefts = [NSMutableSet setWithObjects:[NSExpression expressionForKeyPath:@"name"], [NSExpression expressionForKeyPath:@"source"], nil];
	
	for (NSString * item in [self fieldNames])
	{
		NSExpression * keyPath = [NSExpression expressionForKeyPath:item];
		[lefts addObject:keyPath];
	}
	
	FilterRowTemplate * template = [[FilterRowTemplate alloc] initWithLeftExpressions:[lefts allObjects]
														 rightExpressionAttributeType:NSStringAttributeType
																			 modifier:[original modifier]
																			operators:[original operators]
																			  options:[original options]];
	
	[ruleTemplates removeLastObject];
	[ruleTemplates addObject:template];
	
	[editor setRowTemplates:ruleTemplates];
}	

- (void) awakeFromNib
{
	[self observeValueForKeyPath:@"arrangedObjects" ofObject:fields change:nil context:NULL];
	
	NSDictionary * keyDict = [NSDictionary dictionaryWithObject:@"name" forKey:@"name"];
	
	if (![[fields arrangedObjects] containsObject:keyDict])
		[fields addObject:keyDict];
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	if ([defaults objectForKey:@"task_predicate"] == nil)
	{
		NSPredicate * predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:
																					  [NSPredicate predicateWithFormat:@"(%K != %@)", @"name", @"Task", nil]]];
		
		[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:predicate] forKey:@"task_predicate"];
	}
	
	[fields addObserver:self forKeyPath:@"arrangedObjects" options:0 context:NULL];
	
	[editor bind:@"value" toObject:defaults withKeyPath:@"task_predicate" 
		 options:[NSDictionary dictionaryWithObject:NSKeyedUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
}

@end
